<?php
define('LEGAL_OPER', true);
require('../../includes/admin-init.php');
require(ADMIN_PATH . '/class/CommonData.class.php');
//频道页管理
if($method == 'pagelist'){
	$db = new DB();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$channels = $channelTable->getAll("channel_type=? and is_delete=?", array(5,0));
	if(count($channels)>0){
		$categoryMap = CommonData::getCategoryMap($db->getDb());
		foreach ($channels as $key=>$channel){
			$template = $templateTable->getOne("tp_id=? and is_delete=?", array($channel['tp_id'],0));
			$channels[$key]['tp_name'] = $template['tp_name'];
		}
	}
	$smarty->assign("channels",$channels);
	$smarty->display("cms/channel_page_list.htm");
	exit;
}

//频道页添加页
if($method == 'addpageui'){
	$db = new DB();
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$templates = $templateTable->getAll("is_delete=?", array(0));
	$smarty->assign("templates",$templates);
	$smarty->display("cms/channel_page_add.htm");
	exit;
}

//频道页添加
if($method == 'addpage'){
	$name = $_POST['name'];
	$tpid =  to_int($_POST['tpid']);
	if(isNotEmpty($name) && $tpid>0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$values = array();
		$values['channel_name'] = trim($name);
		$values['tp_id'] = $tpid;
		$values['channel_type'] = 5;
		$values['page_title'] = trim($_POST['title']);
		$values['page_keywords'] = trim($_POST['key']);
		$values['page_desc'] = trim($_POST['desc']);
		try{
			$channelTable->save($values);
			$db->commit();
			$url = '/sys-admin/action/cms/channel.php?met=addpagesuccess&code=1';
			to_redirect($url);
		}catch(Exception $e){
			$db->rollback();
			$url = '/sys-admin/action/cms/channel.php?met=addpagesuccess&code=0';
			to_redirect($url);
		}
		$db->close();
	}else{
		$url = '/sys-admin/action/cms/channel.php?met=addpagesuccess&code=0';
		to_redirect($url);
	}
	exit;
}

if($method == 'addpagesuccess'){
	$arr = array(
			'0'=>'频道页添加失败',
			'1'=>'频道页添加成功'
	);
	$links[0]['text'] = '继续添加';
	$links[0]['href'] = '/sys-admin/action/cms/channel.php?met=addpageui';
	$links[1]['text'] = '返回列表';
	$links[1]['href'] = '/sys-admin/action/cms/channel.php?met=pagelist';
	to_message($smarty, $arr[$_GET['code']],0,$links);
	exit;
}

//频道页修改页
if($method == 'editpageui'){
	$id = to_int($_GET['id']);
	$db = new DB();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$channel = $channelTable->getOne("channel_id=? and is_delete=?", array($id,0));
	$templates = $templateTable->getAll("is_delete=?", array(0));
	$smarty->assign("templates",$templates);
	$smarty->assign("channel",$channel);
	$smarty->display("cms/channel_page_edit.htm");
	exit;
}

//频道页修改
if($method == 'editpage'){
	$name = $_POST['name'];
	$tpid =  to_int($_POST['tpid']);
	$id =  to_int($_POST['id']);
	if(isNotEmpty($name) && $id>0 && $tpid>0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$values = array();
		$values['channel_name'] = trim($name);
		$values['tp_id'] = $tpid;
		$values['page_title'] = trim($_POST['title']);
		$values['page_keywords'] = trim($_POST['key']);
		$values['page_desc'] = trim($_POST['desc']);
		try{
			$channelTable->update($values, 'channel_id=? and is_delete=?', array($id,0));
			$db->commit();
			$url = '/sys-admin/action/cms/channel.php?met=editpagesuccess&code=1';
			to_redirect($url);
		}catch(Exception $e){
			$db->rollback();
			$url = '/sys-admin/action/cms/channel.php?met=editpagesuccess&code=0';
			to_redirect($url);
		}
		$db->close();
	}else{
		$url = '/sys-admin/action/cms/channel.php?met=editpagesuccess&code=0';
		to_redirect($url);
	}
	exit;
}

if($method == 'editpagesuccess'){
	$arr = array(
			'0'=>'频道页修改失败',
			'1'=>'分类页频道修改成功'
	);
	$links[0]['text'] = '返回列表';
	$links[0]['href'] = '/sys-admin/action/cms/channel.php?met=pagelist';
	to_message($smarty, $arr[$_GET['code']],0,$links);
	exit;
}

//分类频道管理
if($method == 'categorylist'){
	$db = new DB();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$channels = $channelTable->getAll("channel_type=? and is_delete=?", array(3,0));
	if(count($channels)>0){
		$categoryMap = CommonData::getCategoryMap($db->getDb());
		foreach ($channels as $key=>$channel){
			$template = $templateTable->getOne("tp_id=? and is_delete=?", array($channel['tp_id'],0));
			$channels[$key]['tp_name'] = $template['tp_name'];
			$channels[$key]['cat_name'] = $categoryMap[$channel['cat_id']]['cat_name'];
		}
	}
	$smarty->assign("channels",$channels);
	$smarty->display("cms/channel_category_list.htm");
	exit;
}


//分类频道添加页
if($method == 'addcategoryui'){
	$db = new DB();
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$templates = $templateTable->getAll("is_delete=?", array(0));
	$categorys = CommonData::getCategorys($db->getDb());
	$smarty->assign("categorys",$categorys);
	$smarty->assign("templates",$templates);
	$smarty->display("cms/channel_category_add.htm");
	exit;
}

//分类频道添加
if($method == 'addcategory'){
	$name = $_POST['name'];
	$catid = to_int($_POST['catid']);
	$tpid =  to_int($_POST['tpid']);
	if(isNotEmpty($name) && $catid>0 && $tpid>0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$channel = $channelTable->getOne("channel_type=? and is_delete=? and cat_id=?", array(3,0,$catid));
		if(isset($channel) && count($channel)>0){
			//分类频道已存在
			$url = '/sys-admin/action/cms/channel.php?met=addcategorysuccess&code=2';
			to_redirect($url);
		}else{
			$values = array();
			$values['channel_name'] = trim($name);
			$values['cat_id'] = $catid;
			$values['tp_id'] = $tpid;
			$values['channel_type'] = 3;
			$values['page_title'] = trim($_POST['title']);
			$values['page_keywords'] = trim($_POST['key']);
			$values['page_desc'] = trim($_POST['desc']);
			try{
				$channelTable->save($values);
				$db->commit();
				$url = '/sys-admin/action/cms/channel.php?met=addcategorysuccess&code=1';
				to_redirect($url);
			}catch(Exception $e){
				$db->rollback();
				$url = '/sys-admin/action/cms/channel.php?met=addcategorysuccess&code=0';
				to_redirect($url);
			}
		}
		$db->close();
	}else{
		$url = '/sys-admin/action/cms/channel.php?met=addcategorysuccess&code=0';
		to_redirect($url);
	}
	exit;
}

if($method == 'addcategorysuccess'){
	$arr = array(
			'0'=>'分类频道添加失败',
			'1'=>'分类频道添加成功',
			'2'=>'分类频道已存在'
	);
	$links[0]['text'] = '继续添加';
	$links[0]['href'] = '/sys-admin/action/cms/channel.php?met=addcategoryui';
	$links[1]['text'] = '返回列表';
	$links[1]['href'] = '/sys-admin/action/cms/channel.php?met=categorylist';
	to_message($smarty, $arr[$_GET['code']],0,$links);
	exit;
}

//分类频道添加页
if($method == 'editcategoryui'){
	$id = to_int($_GET['id']);
	$db = new DB();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$channel = $channelTable->getOne("channel_id=? and is_delete=?", array($id,0));
	$templates = $templateTable->getAll("is_delete=?", array(0));
	$categoryMap = CommonData::getCategoryMap($db->getDb());
	$channel['cat_name'] = $categoryMap[$channel['cat_id']]['cat_name'];
	$smarty->assign("templates",$templates);
	$smarty->assign("channel",$channel);
	$smarty->display("cms/channel_category_edit.htm");
	exit;
}

//分类频道添加
if($method == 'editcategory'){
	$name = $_POST['name'];
	$tpid =  to_int($_POST['tpid']);
	$id =  to_int($_POST['id']);
	if(isNotEmpty($name) && $id>0 && $tpid>0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$values = array();
		$values['channel_name'] = trim($name);
		$values['tp_id'] = $tpid;
		$values['page_title'] = trim($_POST['title']);
		$values['page_keywords'] = trim($_POST['key']);
		$values['page_desc'] = trim($_POST['desc']);
		try{
			$channelTable->update($values, 'channel_id=? and is_delete=?', array($id,0));
			$db->commit();
			$url = '/sys-admin/action/cms/channel.php?met=editcategorysuccess&code=1';
			to_redirect($url);
		}catch(Exception $e){
			$db->rollback();
			$url = '/sys-admin/action/cms/channel.php?met=editcategorysuccess&code=0';
			to_redirect($url);
		}
		$db->close();
	}else{
		$url = '/sys-admin/action/cms/channel.php?met=editcategorysuccess&code=0';
		to_redirect($url);
	}
	exit;
}

if($method == 'editcategorysuccess'){
	$arr = array(
			'0'=>'分类频道修改失败',
			'1'=>'分类频道修改成功'
	);
	$links[0]['text'] = '返回列表';
	$links[0]['href'] = '/sys-admin/action/cms/channel.php?met=categorylist';
	to_message($smarty, $arr[$_GET['code']],0,$links);
	exit;
}

//产品列表管理
if($method == 'productlist'){
	echo 3;
}

//共用内容管理
if($method == 'commonlist'){
	$db = new DB();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$headchannel = $channelTable->getOne("channel_type=? and channel_pos=? and is_delete=?", array(2,1,0));
	$headblocks = array();
	if(isset($headchannel) && count($headchannel)>0){
		$headblocks = $blockTable->getAll("channel_id=?",  array($headchannel['channel_id']));
		if($headchannel['tp_id'] > 0){
			$template = $templateTable->getOne("tp_id=? and is_delete=?", array($headchannel['tp_id'],0));
			$smarty->assign("tp_name_1",$template['tp_name']);
		}
	}
	$footchannel = $channelTable->getOne("channel_type=? and channel_pos=? and is_delete=?", array(2,2,0));
	$footblocks = array();
	if(isset($footchannel) && count($footchannel)>0){
		$footblocks = $blockTable->getAll("channel_id=?", array($footchannel['channel_id']));
	    if($footchannel['tp_id'] > 0){
	    	$template = $templateTable->getOne("tp_id=? and is_delete=?", array($footchannel['tp_id'],0));
	    	$smarty->assign("tp_name_2",$template['tp_name']);
	    }
	}
	
	$smarty->assign("headblocks",$headblocks);
	$smarty->assign("footblocks",$footblocks);
	$smarty->display("cms/channel_common.htm");
	exit;
}

//首页
if($method == 'homepage'){
	$db = new DB();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
	
	$homechannel = $channelTable->getOne("channel_type=? and is_delete=?", array(1,0));
	$homeblocks = array();
	if(isset($homechannel) && count($homechannel)>0){
		$homeblocks = $blockTable->getAll("channel_id=?", array($homechannel['channel_id']));
		$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
		$template = $templateTable->getOne("tp_id=? and is_delete=?", array($homechannel['tp_id'],0));
		$smarty->assign("tp_name",$template['tp_name']);
	}
	$db->close();
	$smarty->assign("homeblocks",$homeblocks);
	$smarty->display("cms/channel_home.htm");
	exit;
}

//删除
if($method == 'delete'){
	$id = to_int($_GET['id']);
	$db = new DB();
	$db->beginTransaction();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	try{
		$channelTable->delete("channel_id=?", array($id));
		$db->commit();
		echo 1;
	}catch(Exception $e){
		$db->rollback();
		echo 0;
	}
	$db->close();
	exit;
}

//首页修改
if($method == 'edithomeui'){
	$db = new DB();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$channel = $channelTable->getOne("channel_type=? and is_delete=?", array(1,0));
	$templates = $templateTable->getAll("is_delete=?", array(0));
	$smarty->assign("templates",$templates);
	$smarty->assign("channel",$channel);
	$smarty->display("cms/channel_home_edit.htm");
	exit;
}

//首页修改
if($method == 'edithome'){
	$tpid =  to_int($_POST['tpid']);
	if($tpid>0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$values = array();
		$values['tp_id'] = $tpid;
		$values['page_title'] = trim($_POST['title']);
		$values['page_keywords'] = trim($_POST['key']);
		$values['page_desc'] = trim($_POST['desc']);
		try{
			$channel = $channelTable->getOne("channel_type=? and is_delete=?", array(1,0));
			if(isset($channel) && count($channel)>0){
				$channelTable->update($values, 'channel_id=? and is_delete=?', array($channel['channel_id'],0));
			}else{
				$values['channel_type'] = 1;
				$values['channel_name'] = '首页';
				$channelTable->save($values);
			}
			$db->commit();
			$url = '/sys-admin/action/cms/channel.php?met=edithomesuccess&code=1';
			to_redirect($url);
		}catch(Exception $e){
			$db->rollback();
			$url = '/sys-admin/action/cms/channel.php?met=edithomesuccess&code=0';
			to_redirect($url);
		}
		$db->close();
	}else{
		$url = '/sys-admin/action/cms/channel.php?met=edithomesuccess&code=0';
		to_redirect($url);
	}
	exit;
}

if($method == 'edithomesuccess'){
	$arr = array(
			'0'=>'首页修改失败',
			'1'=>'首页修改成功'
	);
	$links[0]['text'] = '返回列表';
	$links[0]['href'] = '/sys-admin/action/cms/channel.php?met=homepage';
	to_message($smarty, $arr[$_GET['code']],0,$links);
	exit;
}



//首页修改
if($method == 'commontemplateui'){
	$pos =  to_int($_GET['pos']);
	$db = new DB();
	$db->beginTransaction();
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$channel = $channelTable->getOne("channel_type=? and channel_pos=? and is_delete=?", array(2,$pos,0));
	$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
	$templates = $templateTable->getAll("is_delete=?", array(0));
	$smarty->assign("pos",$pos);
	$smarty->assign("templates",$templates);
	$smarty->assign("channel",$channel);
	$smarty->display("cms/channel_common_template.htm");
	exit;
}

//首页修改
if($method == 'commontemplate'){
	$tpid =  to_int($_POST['tpid']);
	$pos =  to_int($_POST['pos']);
	if($tpid>0 && $pos>0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$values = array();
		$values['tp_id'] = $tpid;
		try{
			$channel = $channelTable->getOne("channel_type=? and channel_pos=? and is_delete=?", array(2,$pos,0));
			if(isset($channel) && count($channel)>0){
				$channelTable->update($values, 'channel_id=? and is_delete=?', array($channel['channel_id'],0));
			}else{
				if($pos===1){
					$values['channel_name'] = '共用头部';
				}else if($pos===2){
					$values['channel_name'] = '共用尾部';
				}
				$values['channel_type'] = 2;
				$values['channel_pos'] = $pos;
				$channelTable->save($values);
			}
			$db->commit();
			$url = '/sys-admin/action/cms/channel.php?met=commontemplatesuccess&code=1';
			to_redirect($url);
		}catch(Exception $e){
			$db->rollback();
			$url = '/sys-admin/action/cms/channel.php?met=commontemplatesuccess&code=0';
			to_redirect($url);
		}
		$db->close();
	}else{
		$url = '/sys-admin/action/cms/channel.php?met=commontemplatesuccess&code=0';
		to_redirect($url);
	}
	exit;
}

if($method == 'commontemplatesuccess'){
	$arr = array(
			'0'=>'修改失败',
			'1'=>'修改成功'
	);
	$links[0]['text'] = '返回共用内容管理 ';
	$links[0]['href'] = '/sys-admin/action/cms/channel.php?met=commonlist';
	to_message($smarty, $arr[$_GET['code']],0,$links);
	exit;
}
